package com.xx.doublexchat.common.user.service.impl;

import com.xx.doublexchat.common.user.cache.UserCache;
import com.xx.doublexchat.common.user.domain.enums.RoleEnum;
import com.xx.doublexchat.common.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private UserCache userCache;

    /**
     * 判断是否拥有这个权限 ---> 超级管理员默认有
     * @param uid
     * @param roleEnum
     * @return
     */
    @Override
    public boolean hasPower(Long uid, RoleEnum roleEnum) {
        Set<Long> roleSet = userCache.getRoleSet(uid);
        return isAdmin(roleSet) || roleSet.contains(roleEnum.getId());
    }

    /**
     * 判断是否为超级管理员
     * @param roleSet
     * @return
     */
    private boolean isAdmin(Set<Long> roleSet) {
        return roleSet.contains(RoleEnum.ADMIN.getId());
    }
}
